package com.khyr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.entity.News;
import com.khyr.service.NewsService;
import com.khyr.utils.DemoUtil;
import com.khyr.vo.NewsVo;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 宋一恒
 * @version 1.0
 */
@RestController
@RequestMapping("/systemManager/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    //查询所有
    @PostMapping("/getNewsByParams")
    public R<Page<News>> getNewsByParams(@RequestBody NewsVo newsVo){
        return newsService.getNewsByParams(newsVo);
    }

    //添加
    @PostMapping("/addNews")
    public R<String> addNews(@RequestBody News news){
        return newsService.addNews(news);
    }

    //修改
    @PutMapping("/updateNews")
    public R<String> updateNews(@RequestBody News news){
        return newsService.updateNews(news);
    }


    //删除
    @DeleteMapping("/deleteNewsByIds/{newsId}")
    public R<String> deleteNewsByIds(@PathVariable Long newsId){
        return newsService.deleteNewsByIds(newsId);
    }

    //上传图片
    @PostMapping("/upload")
    public R uploadhead(MultipartFile file) throws Exception {
        //上传图片
        DemoUtil demoUtil = new DemoUtil();
        String url = demoUtil.upload(file);
        return new R(200,"上传成功",url);
    }
}
